package cn.lili.controller.user;

import cn.lili.common.aop.annotation.DemoSite;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.service.UserService;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.modules.store.entity.dos.StoreBind;
import cn.lili.modules.store.entity.dto.*;
import cn.lili.modules.store.entity.vos.StoreLicenceVO;
import cn.lili.modules.store.entity.vos.StoreManagementCategoryVO;
import cn.lili.modules.store.entity.vos.StoreVO;
import cn.lili.modules.store.service.StoreBindService;
import cn.lili.modules.store.service.StoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * 店铺管理接口
 *
 * @author Bulbasaur
 * @since 2020/12/6 16:09
 */
@Api(tags = "店铺管理接口")
@RestController
@RequestMapping("/user/store/bind")
@RequiredArgsConstructor
@Validated
public class StoreBindController {

    private final StoreBindService storeBindService;

    /**
     * 分页查询接口
     */
    @GetMapping("/page")
    public ResultMessage<Object> page(StoreBindSearchParams storeBindSearch) {
        Page<StoreBind> storeBindPage = new Page<>(storeBindSearch.getPageNumber(), storeBindSearch.getPageSize());
        LambdaQueryWrapper<StoreBind> storeBindLambdaQueryWrapper = new LambdaQueryWrapper<>();
        storeBindLambdaQueryWrapper.eq(storeBindSearch.getStatus() != null , StoreBind::getStatus, storeBindSearch.getStatus());
        storeBindLambdaQueryWrapper.eq(StringUtils.isNotEmpty(storeBindSearch.getFyAccount()), StoreBind::getFyAccount, storeBindSearch.getFyAccount());

        storeBindLambdaQueryWrapper.eq(StoreBind::getStoreId, UserContext.getCurrentUser().getExtendId());
        return ResultUtil.data(storeBindService.page(storeBindPage, storeBindLambdaQueryWrapper));
    }

    @ApiOperation(value = "添加店铺绑定关系")
    @PostMapping(value = "/add")
    public ResultMessage<Boolean> add(@Valid @RequestBody StoreBindDTO storeBindDTO) {
        if (StringUtils.isEmpty(storeBindDTO.getFyAccount())) {
            throw new ServiceException("飞语账号未填写");
        }
        if (storeBindDTO.getStatus() == null) {
            throw new ServiceException("启用或禁用未填写");
        }
        return ResultUtil.data(storeBindService.add(storeBindDTO));
    }

    @ApiOperation(value = "编辑店铺绑定关系")
    @ApiImplicitParam(name = "storeId", value = "id", required = true, paramType = "path", dataType = "String")
    @PutMapping(value = "/{id}")
    public ResultMessage<Boolean> edit(@Valid @PathVariable String id, StoreBindEditDTO storeBindEditDTO) {
        storeBindEditDTO.setId(id);
        return ResultUtil.data(storeBindService.edit(storeBindEditDTO));
    }


    @ApiOperation(value = "关闭店铺绑定关系")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/disable/{id}")
    public ResultMessage<Store> disable(@PathVariable String id) {
        storeBindService.disable(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "开启店铺绑定关系")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/enable/{id}")
    public ResultMessage<Store> enable(@PathVariable String id) {
        storeBindService.enable(id);
        return ResultUtil.success();
    }

    /**
     * 删除
     * @param id 删除id
     * @return 是否成功
     */
    @PostMapping("/delete")
    public ResultMessage<Object> delete(@RequestParam String id) {
        storeBindService.removeById(id);
        return ResultUtil.success();
    }


    /**
     * 获取店铺对应的客服
     * @param storeId 店铺id
     * @return 店铺客服
     */
    @GetMapping("/getStoreCustomerService")
    public ResultMessage<Object> getStoreCustomerService(@RequestParam String storeId) {
        return ResultUtil.data(storeBindService.getStoreCustomerService(storeId));
    }

}
